import { HTTP } from "../../utils/http"
import api from '../../utils/api.js'
import { config } from '../../utils/config'
import common from '../../utils/common'
let http = new HTTP()
Page({
    data: {
        date: common.date.format_month,
        dotMonth: common.date.format_dot_month, //年和月
        endMonth: common.date.format_month,  //不许超出这个月的时间
        imgUrl: config.imgUrl,
        gegou_num: 0,
        page : 1,
        list : [],
        user_token: wx.getStorageSync('token')
    },
    bindDateChange: function(e) {
        let timeDay = e.detail.value;
        this.setData({
            page: 1,
            list: [],
            dotMonth: timeDay.replace('-', '.'),
        })
        this.queryDou(this.data.user_token, timeDay)
    },
    onLoad: function() {
        this.queryDou(this.data.user_token, this.data.endMonth)
        this.getCoins(this.data.user_token)
        
    },
    queryDou(user_token, timeDay) {
        let that = this
        http.request({
            url: api.getCoins,
            data: {
                token: user_token,
                month: timeDay,
                page: that.data.page
            },
            success: (res) => {
                console.log(res)
                this.setData({
                    list: that.data.list.concat(res.data.list),
                    date: timeDay,
                    totalPage :  Math.ceil(res.data.total / res.data.per_page)
                })
            }
        })

    },
    toDisable(){
        wx.navigateTo({
            url : '/pages/gedouDetail/disable/disable'
        })
     },
    //虚拟模态框
    popConfirm: function() {
        wx.showModal({
            content: '每评论一条订单即可获得1个歌豆，每天一枚',
            showCancel: false,
            confirmText: '我知道了',
            success: function(res) {
                if (res.confirm) {
                    console.log('点击确认回调')
                } else {
                    console.log('点击取消回调')
                }
            }
        })
    },
    //获取歌豆
    getCoins(user_token) {
        http.request({
            url: api.accountCoins,
            data: {
                token: user_token
            },
            success: (res) => {
                console.log(res)
                if (res.code == 200) {
                    this.setData({
                        gegou_num: res.data.coins,
                        frozen_coins : res.data.frozen_coins
                    })
                }
            }
        })
    },
    onReachBottom: function() {
        if (this.data.page < this.data.totalPage) {
            this.setData({
                page: this.data.page + 1
            })
            let timeDay = this.data.date
            this.queryDou(this.data.user_token, timeDay)
        }
    },
})